package com.test.burnincash.aaahpoppomelo.activity;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;

import androidx.fragment.app.FragmentTransaction;

import com.test.burnincash.R;
import com.test.burnincash.aaahpoppomelo.base.BaseActivity_hpoppomelo;
import com.test.burnincash.aaahpoppomelo.fragment.OrderListFragment_hpoppomelo;
import com.test.burnincash.aaahpoppomelo.fragment.UserFragment_hpoppomelo;
import com.test.burnincash.aaahpoppomelo.util.http_hpoppomelo.HttpManager_hpoppomelo;

public class MainActivity_hpoppomelo extends BaseActivity_hpoppomelo {

    private ViewGroup fragmentLayout;
    private ImageView orderListIv,userIv;

    private OrderListFragment_hpoppomelo orderListFragment;
    private UserFragment_hpoppomelo userFragment;

    private boolean fragmentFlag = true;//true:dataListFragment     false:userFragment

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_hpoppomelo);
        init();
    }

    private void init(){

        fragmentLayout = findViewById(R.id.activity_main_hpoppomelo_layout_fragment);
        ImageButton addOrderIb = findViewById(R.id.activity_main_hpoppomelo_ib_add_order);
        View orderListLayout = findViewById(R.id.activity_main_hpoppomelo_layout_order_list);
        View userLayout = findViewById(R.id.activity_main_hpoppomelo_layout_user);
        orderListIv = findViewById(R.id.activity_main_hpoppomelo_iv_order_list);
        userIv = findViewById(R.id.activity_main_hpoppomelo_iv_user);

        orderListFragment = new OrderListFragment_hpoppomelo();
        userFragment = new UserFragment_hpoppomelo();

        orderListLayout.setOnClickListener(v -> changeToOrderList());
        userLayout.setOnClickListener(v -> changeToUser());

        addOrderIb.setOnClickListener(v -> startActivity_burnin(AddOrderActivity_hpoppomelo.class));

    }

    @Override
    protected void onResume() {
        super.onResume();
        updatePage();
    }

    private void updatePage(){

        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();

        if (orderListFragment!=null) fragmentTransaction.remove(orderListFragment);
        if (userFragment!=null) fragmentTransaction.remove(userFragment);

        fragmentTransaction.add(fragmentLayout.getId(),orderListFragment)
                .add(fragmentLayout.getId(),userFragment);

        if (fragmentFlag) fragmentTransaction.show(orderListFragment).hide(userFragment);
        else fragmentTransaction.show(userFragment).hide(orderListFragment);

        fragmentTransaction.commit();

    }

    private void changeToOrderList(){

        orderListIv.setBackgroundResource(R.drawable.icon_tab_data_true_hpoppomelo);
        userIv.setImageResource(R.drawable.icon_tab_user_false_hpoppomelo);

        getSupportFragmentManager().beginTransaction().show(orderListFragment).hide(userFragment).commit();

        fragmentFlag = true;
    }

    private void changeToUser(){

        orderListIv.setBackgroundResource(R.drawable.icon_tab_data_false_hpoppomelo);
        userIv.setImageResource(R.drawable.icon_tab_user_true_hpoppomelo);

        getSupportFragmentManager().beginTransaction().show(userFragment).hide(orderListFragment).commit();

        fragmentFlag = false;
    }

    public void onDataUploadSuccess(){
        HttpManager_hpoppomelo.getInstance().getData();
    }

    public void onDataGet(){
        if (orderListFragment!=null) orderListFragment.updateOrderList();
    }

}